package site

import (
	"fmt"
	"hft/Http/Services/site"
	siterequest "hft/Http/Services/site/site_request"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"hft/tools"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Site struct{}

func (*Site) GetCanSetRegionByAccount(c *gin.Context) {

	var paginate siterequest.SiteReq
	c.ShouldBind(&paginate)

	list, err := site.SiteService.GetSiteAccountCanSet(&paginate)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

// 设置站点
func (*Site) SetSite(c *gin.Context) {
	var data *siterequest.SetSite
	c.ShouldBindJSON(&data)

	err := site.SiteService.SetSite(data)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

// 解绑站点
func (*Site) ReleaseSite(c *gin.Context) {
	var data *siterequest.SetSite
	c.ShouldBindJSON(&data)
	err := site.SiteService.ReleaseSite(data)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.Ok(c)
}

// 获取账号管理的站点
func (*Site) GetSiteList(c *gin.Context) {
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	list, err := site.SiteService.GetSiteList(claims.BaseClaims)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	utils.OkWithData(list, c)
}

func (*Site) SetNowSiteLogin(c *gin.Context) {
	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))
	claims, err := tools.GetClaims(c)
	if err != nil {
		utils.FailWithMessage(e.InValidToken, c)
		return
	}
	err = site.SiteService.SetNowSiteLogin(claims.BaseClaims, id)
	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}
	// 设置当前登录站点
	global.OC_REDIS.Set(fmt.Sprintf("SITE_LOGIN-%v", claims.BaseClaims.Username), id, -1)
	utils.Ok(c)
}

var SiteController = new(Site)
